﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Web
Imports System.Globalization
Imports System

Namespace Infrastructure

    Public Class SqlAzureODataServiceRequestValidator
        Implements IStorageRequestValidator
        Private Const BaseUrl As String = "/SqlAzureSampleODataService"

        Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository

        Public Sub New()
            Me.New(New SqlDataContext())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository)
            Me.userPrivilegesRepository = userPrivilegesRepository
        End Sub

        Public Function DoesRequestApply(ByVal resourceUri As Uri) As Boolean Implements IStorageRequestValidator.DoesRequestApply
            Return resourceUri.AbsolutePath.StartsWith(BaseUrl, StringComparison.OrdinalIgnoreCase)
        End Function

        Public Function ValidateRequest(ByVal userId As String, ByVal request As HttpRequest) As Boolean Implements IStorageRequestValidator.ValidateRequest
            Return Me.CanUseSqlAzureODataService(userId)
        End Function

        Private Function CanUseSqlAzureODataService(ByVal userId As String) As Boolean
            Return Me.userPrivilegesRepository.HasUserPrivilege(userId, PrivilegeConstants.SqlUsagePrivilege)
        End Function
    End Class
End Namespace